using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ConnectCsharpToMysql.objects;

namespace ConnectCsharpToMysql
{
    public partial class Form1 : Form
    {
        private DBConnect dbConnect;
        private List<String> listTableName = new List<string>();

        public Form1()
        {
            InitializeComponent();

            dbConnect = new DBConnect();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // show list table name
            this.listTableName = Database.getListTableName();
            lsbTable.DataSource = this.listTableName;
            // show list category
            lsbCategory.DataSource = new DBConnect().Select("SELECT * FROM tbcategory"); ;
            lsbCategory.DisplayMember = "nameCategory";
            lsbCategory.ValueMember = "idCategory";
            // show list video
            lsbVideo.DataSource = new DBConnect().Select("SELECT * FROM tbvideo");
            lsbVideo.DisplayMember = "nameVideo";
            lsbVideo.ValueMember = "idVideo";
        }
        // get datatime now
        private String getDateTimeNow()
        {
            //yyyy-MM-dd hh:mm:ss
            DateTime now = DateTime.Now;
            String dateTime = now.Year + "-" + now.Month + "-" + now.Day + " " + now.Hour + ":" + now.Minute + ":" + now.Second;
            return dateTime;
        }
        private void lsbTable_Click(object sender, EventArgs e)
        {
            if (lsbTable.SelectedItems.Count > 0)
            {
                String strTableName = lsbTable.SelectedValue.ToString();
                dgDisplay.DataSource = new DBConnect().Select("SELECT * FROM " + strTableName);
            }
        }

        /* Category Button Control */
        // Save button
        private void btnCategorySave_Click(object sender, EventArgs e)
        {
            Category category = new Category();
            category.idCategory = txtCategoryId.Text.Trim();
            category.nameCategory = txtCategoryName.Text.Trim();
            category.desCategory = txtCategoryDes.Text.Trim();
            category.avatar = txtCategoryAvatar.Text.Trim();
            category.createDate = getDateTimeNow();
            category.status = txtCategoryStatus.Text.Trim();
            // to save to database
            if (category.idCategory.Equals(String.Empty))
            {
                // insert new category
                if (category.insert())
                    MessageBox.Show("Insert new category success!");
                else
                    MessageBox.Show("Insert new category fail!");
            }
            else
            {
                // update category
                if (category.update())
                    MessageBox.Show("Update category success!");
                else
                    MessageBox.Show("Update category fail!");
            }
        }
        // delete category button
        private void btnCategoryDel_Click(object sender, EventArgs e)
        {
            Category category = new Category();
            category.idCategory = txtCategoryId.Text.Trim();
            if (!category.idCategory.Equals(String.Empty))
            {
                if (MessageBox.Show("Do you want to delete this Category?", "Sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (category.delete())
                        MessageBox.Show("Delete this Category success!");
                    else
                        MessageBox.Show("Delete this Category fail!");
                }
            }
        }
        // clear form Category button
        private void btnCategoryClear_Click(object sender, EventArgs e)
        {
            txtCategoryId.Text = string.Empty;
            txtCategoryName.Text = string.Empty;
            txtCategoryDes.Text = string.Empty;
            txtCategoryAvatar.Text = string.Empty;
            txtCategoryCreateDate.Text = getDateTimeNow();
            txtCategoryStatus.Text = "1";
        }
        /* Video Button Control */
        // save button
        private void btnVideoSave_Click(object sender, EventArgs e)
        {
            Video video = new Video();
            video.idVideo = txtVideoId.Text.Trim();
            video.nameVideo = txtVideoName.Text.Trim();
            video.trailerVideo = txtVideoTrailer.Text.Trim();
            video.desVideo = txtVideoDes.Text.Trim();
            video.avatar = txtVideoAvatar.Text.Trim();
            video.status = txtVideoStatus.Text.Trim();
            video.createDate = getDateTimeNow();
            video.totalPart = txtVideoTotalPart.Text.Trim();
            video.idCategory = lsbCategory.SelectedValue.ToString();
            // save video info
            if (video.idVideo.Equals(string.Empty))
            {
                // insert new Video
                if (video.insert())
                    MessageBox.Show("Insert new Video success!");
                else
                    MessageBox.Show("Insert new Video fail!");
            }
            else
            {
                // update Video
                if (video.insert())
                    MessageBox.Show("Update Video success!");
                else
                    MessageBox.Show("Update Video fail!");
            }
        }
        // Delete button
        private void btnVideoDel_Click(object sender, EventArgs e)
        {
            Video video = new Video();
            video.idVideo = txtVideoId.Text.Trim();
            if (!video.idVideo.Equals(String.Empty))
            {
                if (MessageBox.Show("Do you want to delete this Video?", "Sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (video.delete())
                        MessageBox.Show("Delete this Video success!");
                    else
                        MessageBox.Show("Delete this Video fail!");
                }
            }
        }
        // Clear Video form
        private void btnVideoClear_Click(object sender, EventArgs e)
        {
            txtVideoId.Text = string.Empty;
            txtVideoName.Text = string.Empty;
            txtVideoTrailer.Text = string.Empty;
            txtVideoDes.Text = string.Empty;
            txtVideoAvatar.Text = string.Empty;
            txtVideoStatus.Text = "1";
            txtVideoCreateDate.Text = getDateTimeNow();
            txtVideoTotalPart.Text = "0";
        }
        /* Part Video button Control */
        // save button
        private void btnPartVideoSave_Click(object sender, EventArgs e)
        {
            PartVideo partVideo = new PartVideo();
            partVideo.idPartVideo = txtPartVideoId.Text.Trim();
            partVideo.namePartVideo = txtPartVideoName.Text.Trim();
            partVideo.desPartVideo = txtPartVideoDes.Text.Trim();
            partVideo.partVideo = txtPartVideo.Text.Trim();
            partVideo.status = txtPartVideoStatus.Text.Trim();
            partVideo.orders = txtPartVideoOrder.Text.Trim();
            partVideo.createDate = getDateTimeNow();
            partVideo.idVideo = lsbVideo.SelectedValue.ToString();
            // save video info
            if (partVideo.idPartVideo.Equals(string.Empty))
            {
                // insert new PartVideo
                if (partVideo.insert())
                    MessageBox.Show("Insert new PartVideo success!");
                else
                    MessageBox.Show("Insert new PartVideo fail!");
            }
            else
            {
                // update PartVideo
                if (partVideo.insert())
                    MessageBox.Show("Update PartVideo success!");
                else
                    MessageBox.Show("Update PartVideo fail!");
            }
        }
        /* get info form data grid */
        private void dgDisplay_Click(object sender, EventArgs e)
        {
            //testMethod();
            if (dgDisplay.SelectedCells.Count > 0)
            {
                //testMethod();
                //String tableName = lsbTable.SelectedValue.ToString();
                //MessageBox.Show(dgDisplay.SelectedRows[0].Cells[0].ToString());
            }
        }

        /* Backup - Restore*/
        //Backup button clicked
        private void bBackup_Click(object sender, EventArgs e)
        {
            dbConnect.Backup();
        }

        //Restore button clicked
        private void bRestore_Click(object sender, EventArgs e)
        {
            dbConnect.Restore();
        }

        // test method
        private void testMethod()
        {
            MessageBox.Show("test");
        }

    }
}